package ch04;

import java.util.ArrayList;
import java.util.List;

public class DiGraph {
    private final int V;
    private int E;
    private List<Integer>[] adj;


    public DiGraph(int v) {
        this.V = v;
        this.E = 0;
        adj = new ArrayList[V];
        for (int i = 0; i < adj.length; i++) {
            adj[i] = new ArrayList<>();
        }
    }

    public int V() {
        return this.V;
    }

    public int E() {
        return this.E;
    }

    public void addEdge(int v, int w) {
        adj[v].add(w);

        E++;
    }

    public Iterable<Integer> adj(int v) {
        return adj[v];
    }

    public DiGraph reverse() {
        DiGraph diGraph = new DiGraph(V);
        for (int i = 0; i < V; i++) {
            for (Integer t : adj[i]) {
                diGraph.adj[t].add(i);
            }
        }
        return diGraph;
    }

    public static DiGraph getDigGraph() {
        DiGraph diGraph = new DiGraph(15);
        diGraph.addEdge(0, 1);
        diGraph.addEdge(0, 5);
        diGraph.addEdge(0, 6);
        diGraph.addEdge(2, 0);
        diGraph.addEdge(2, 3);
        diGraph.addEdge(3, 5);
        diGraph.addEdge(5, 4);
        diGraph.addEdge(4, 3);
        diGraph.addEdge(6, 4);
        diGraph.addEdge(8, 7);
        diGraph.addEdge(7, 6);
        diGraph.addEdge(6, 9);
        diGraph.addEdge(9, 10);
        diGraph.addEdge(9, 11);
        diGraph.addEdge(11, 12);
        return diGraph;
    }

}
